/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.model;

import com.db.SqlManager;
import com.exception.DataException;
import com.javabeans.Transaccion;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Administrador
 */
public class AdminTransaccion implements TransaccionManager{
    private Connection c;
    public AdminTransaccion(Connection c){
        this.c = c;
    }
    @Override
    public int getIdTransaccion(int idPeriodo) throws DataException {
        int res = 0;
        ResultSet rs = null;
        PreparedStatement preSt = null;
        String query = "";
        try{
            query = "SELECT IFNULL(MAX(IDTRANSACCION)+1,1) IDTRANSACCION";
            query += " FROM TRANSACCION";
            query += " WHERE IDPERIODO="+idPeriodo;
            preSt = c.prepareStatement(query);
            rs = preSt.executeQuery();
            rs.next();
            res = rs.getInt("IDTRANSACCION");
        }catch(SQLException e){
            throw new DataException(" AdminTransaccion -> getIdTransaccion(idPeriodo) "+e.getMessage());
        }finally{
            SqlManager.closeRs(rs);
            SqlManager.closePreSt(preSt);
        }
        return res;
    }
    @Override
    public void saveTransaccion(Transaccion t) throws DataException {
        PreparedStatement preSt = null;
        try{
            String query = "INSERT INTO TRANSACCION VALUES";
            query += "("+t.getIdPeriodo();
            query += ","+t.getIdTransaccion()+",'"+t.getFecha();
            query += "',"+t.getMonto();
            query += ",'"+t.getObservacion();
            query += "',"+t.getIdOperacion();
            query += ","+t.getIdFactura();
            query += ","+t.getIdUsuario();
            query += ","+t.getIdFacultad();
            query += ",'"+t.getIp()+"')";            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException e){
            throw new DataException(" AdminTransaccion -> saveTransaccion(Transaccion t) SQLException = "+e.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    @Override
    public void saveTransacciones(List<Transaccion> transacciones) throws DataException {
        PreparedStatement preSt = null;
        int i = 0;
        String query = "INSERT INTO TRANSACCION VALUES";
        try{
            for(Transaccion t : transacciones){
                query += "("+t.getIdPeriodo();
                query += ","+t.getIdTransaccion()+",'"+t.getFecha();
                query += "',"+t.getMonto();
                query += ",'"+t.getObservacion();
                query += "',"+t.getIdOperacion();
                query += ","+t.getIdFactura();
                query += ","+t.getIdUsuario();
                query += ","+t.getIdFacultad();
                query += ",'"+t.getIp()+"'),";
                i++;
            }
            if(i > 0){
                i = query.lastIndexOf(",");
                query = query.substring(0, i);
            }            
            preSt = c.prepareStatement(query);
            preSt.executeUpdate();
        }catch(SQLException e){
            throw new DataException(" AdminTransaccion -> saveTransacciones(List<Transaccion> transacciones) "+e.getMessage());
        }finally{
            SqlManager.closePreSt(preSt);
        }
    }
    
}
